<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h4 class="mb-0">用户登录</h4>
            </div>
            <div class="card-body">
                <div id="errorAlert" class="alert alert-danger d-none" role="alert"></div>
                <form id="loginForm">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div>
                    <button type="submit" class="btn btn-primary">登录</button>
                    <a href="/quiz_system/user/register" class="btn btn-link">没有账号？立即注册</a>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
    $('#loginForm').on('submit', function(e) {
        e.preventDefault();
        $.post('${pageContext.request.contextPath}/user/login', $(this).serialize(), function(response) {
            if (response.code === 200) {
                if (response.data.redirectUrl) {
                    // 如果有重定向URL，则跳转到之前的页面
                    window.location.href = '${pageContext.request.contextPath}' + response.data.redirectUrl;
                } else {
                    // 否则跳转到首页
                    window.location.href = '${pageContext.request.contextPath}/';
                }
            } else {
                alert(response.message);
            }
        });
    });
});
</script> 